Ext.ns("PersonalTipsView");
Ext.DataView.LabelEditor = Ext.extend(Ext.Editor, {
        alignment : "tl-tl",
        hideEl : false,
        cls : "x-small-editor",
        shim : false,
        completeOnEnter : true,
        cancelOnEsc : true,
        labelSelector : "div.thumb-wrap",
        constructor : function (a, b) {
            Ext.DataView.LabelEditor.superclass.constructor.call(this, b || new Ext.form.TextArea({
                    allowBlank : false,
                    growMin : 90,
                    growMax : 240,
                    grow : true,
                    selectOnFocus : true
                }), a);
        },
        init : function (a) {
            this.view = a;
            a.on("afterrender", this.initEditor, this);
            this.on("complete", this.onSave, this);
        },
        initEditor : function () {
            this.view.on({
                scope : this,
                containerclick : this.doBlur,
                click : this.doBlur
            });
            var a = this.view.getEl();
            a.on("dblclick", this.onMouseDown, this, {
                delegate : this.labelSelector
            });
        },
        doBlur : function () {
            if (this.editing) {
                this.field.blur();
            }
        },
        onMouseDown : function (h, g) {
            if (!h.ctrlKey && !h.shiftKey) {
                var f = this.view.findItemFromChild(g);
                h.stopEvent();
                var i = this.view.indexOf(f);
                var b = this.view.store.getAt(i);
                this.startEdit(g, b.data[this.dataIndex]);
                var a = f.scrollHeight;
                var d = f.scrollWidth;
                if (Ext.isIE) {
                    d = d + 2;
                    if (Ext.isIE6 || Ext.isIE7) {
                        var c = f.parentNode;
                        a = c.scrollHeight;
                    } else {
                        a = a + 2;
                    }
                }
                this.setSize(d, a);
                this.activeRecord = b;
            } else {
                h.preventDefault();
            }
        },
        onSave : function (a, b) {
            this.activeRecord.set(this.dataIndex, b);
            this.view.store.fireEvent("recordchange", this.view.store);
        }
    });
PersonalTipsView = Ext.extend(Ext.Panel, {
        maxLevel : 1,
        topbar : null,
        dataView : null,
        store : null,
        curRecord : null,
        curLevel : null,
        constructor : function (a) {
            Ext.applyIf(this, a);
            this.initUI();
            PersonalTipsView.superclass.constructor.call(this, {
                id : "PersonalTipsView",
                title : "个人便签",
                iconCls : "tipsTile",
                layout : "fit",
                tbar : this.topbar,
                autoScroll : false,
                items : [this.dataView]
            });
        },
        initUI : function () {
            this.store = new Ext.data.JsonStore({
                    url : __ctxPath + "/info/listAppTips.do",
                    root : "result",
                    totalProperty : "totalCounts",
                    remoteSort : true,
                    fields : ["tipsId", "tipsName", "content", {
                            name : "disheight",
                            type : "int"
                        }, {
                            name : "diswidth",
                            type : "int"
                        }, {
                            name : "disleft",
                            type : "int"
                        }, {
                            name : "distop",
                            type : "int"
                        }, "dislevel", "createTime"]
                });
            this.store.setDefaultSort("tipsId", "asc");
            this.store.load({
                params : {
                    start : 0,
                    limit : 1000
                }
            });
            this.store.on("load", this.loadAction, this);
            this.store.on("recordchange", this.recordChange, this);
            this.dataView = new Ext.DataView({
                    id : "dataView",
                    autoScroll : false,
                    store : this.store,
                    itemSelector : "div.thumb-wrap",
                    multiSelect : false,
                    singleSelect : true,
                    overClass : "setDiv",
                    plugins : [new Ext.DataView.LabelEditor({
                            dataIndex : "content"
                        })],
                    tpl : new Ext.XTemplate('<div id="basicDiv" style="width:100%;overflow:hidden;height:100%;">', '<div id="inputDiv" style="position: absolute;width:100%;height:100%;"></div>', '<tpl for=".">', '<div id={tipsName} class="thumb-wrap tipDiv" style="width:{diswidth}px;height:{disheight}px;left:{disleft}px;top:{distop}px;z-index:{dislevel};"><span id={tipsName}a class="x-linkdel"><button class="tipsClose" value="" onclick="PersonalTipsView.prototype.deleteTips({tipsId},\'{tipsName}\');" title="删除"></button></span><span class="x-editable">{content}</span><span class="x-timelabel">{createTime}</span></div>', "</tpl>", "</div>"),
                    scope : this,
                    emptyText : '<div id="basicDiv" style="width:100%;overflow:hidden;height:100%;"><div id="inputDiv" style="position: absolute;width:100%;height:100%;"></div></div>',
                    listeners : {
                        scope : this,
                        "afterrender" : {
                            fn : this.bodyRender,
                            scope : this
                        },
                        "mouseenter" : {
                            fn : this.mouseenter,
                            scope : this
                        },
                        "mouseleave" : {
                            fn : this.mouseleave,
                            scope : this
                        },
                        "click" : {
                            fn : this.clickView,
                            scope : this
                        }
                    }
                });
            this.topbar = new Ext.Toolbar({
                    items : ['<font color="red">提示：在空白面板双击输入！</font>', "->", {
                            xtype : "button",
                            text : "一键清除",
                            iconCls : "btn-delete",
                            scope : this,
                            handler : function () {
                                if (this.store.getCount() > 0) {
                                    PersonalTipsView.prototype.deleteTips("all");
                                } else {
                                    Ext.ux.Toast.msg("信息", "没有记录可删除!");
                                }
                            }
                        }
                    ]
                });
        },
        loadAction : function (a) {
            if (a.getCount() > 0) {
                for (var b = 0; b < a.getCount(); b++) {
                    var c = a.getAt(b).get("tipsName");
                    this.resizable(c);
                }
            }
        },
        recordChange : function (a) {
            this.saveRecord(a);
        },
        saveAction : function (a) {
            this.saveRecord(a);
        },
        bodyContextClick : function (c, j, b) {
            if (!c.ctrlKey && !c.shiftKey) {
                var a = Ext.get("basicDiv");
                var i = this.getInputCmp();
                var h = this.dataView.findItemFromChild(j);
                if (h == null) {
                    var f = c.getPageX();
                    var d = c.getPageY();
                    i.setStyle("top", c.getPageY() - a.getTop() + "px");
                    i.setStyle("left", c.getPageX() - a.getLeft() + "px");
                    var g = new Ext.DataView.LabelEditor();
                    g.setSize(200, 100);
                    g.on("complete", function (l, m) {
                        if (h == null) {
                            var o = l.el.getBox();
                            var e = {
                                tipsId : "-1",
                                tipsName : "tips"
                                +new Date().format("YmdHisu"),
                                content : m,
                                diswidth : o.width,
                                disheight : o.height,
                                disleft : f - a.getLeft(),
                                distop : d - a.getTop(),
                                dislevel : this.maxLevel + 1,
                                createTime : new Date().format("Y-m-d H:i:s")
                            };
                            var n = new this.store.recordType(e);
                            this.store.add(n);
                            this.dataView.refresh();
                            if (this.store.getCount() > 0) {
                                for (var k = 0; k < this.store.getCount(); k++) {
                                    var q = this.store.getAt(k).get("tipsName");
                                    this.resizable(q);
                                }
                            }
                            this.store.fireEvent("recordchange", this.store);
                        }
                    }, this);
                    g.startEdit(j, "");
                    i.setStyle("top", "0px");
                    i.setStyle("left", "0px");
                }
            }
        },
        bodyRender : function (a) {
            a.getEl().on("dblclick", this.bodyContextClick, this);
        },
        saveRecord : function (b) {
            var e = [];
            for (var c = 0; c < b.getCount(); c++) {
                var a = b.getAt(c);
                var d = a.data.tipsId;
                if (this.curTipsId != undefined || (a.dirty || d == -1 || this.curTipsId == d)) {
                    e.push(a.data);
                    a.dirty = false;
                    this.curTipsId = null;
                }
            }
            if (e.length == 0) {
                return;
            }
            Ext.Ajax.request({
                method : "post",
                url : __ctxPath + "/info/saveAppTips.do",
                success : function (h) {
                    for (var g = 0; g < b.getCount(); g++) {
                        var f = b.getAt(g);
                        var j = f.data.tipsId;
                        if (j == -1) {
                            f.set("tipsId", "-2");
                            f.dirty = false;
                        }
                    }
                    b.reload({
                        params : {
                            start : 0,
                            limit : 1000
                        }
                    });
                },
                failure : function (f) {
                    b.reload({
                        params : {
                            start : 0,
                            limit : 1000
                        }
                    });
                    Ext.MessageBox.show({
                        title : "操作信息",
                        msg : "信息保存出错，请联系管理员！",
                        buttons : Ext.MessageBox.OK,
                        icon : "ext-mb-error"
                    });
                },
                params : {
                    data : Ext.encode(e),
                    curTipsId : this.curTipsId,
                    curWidth : this.curWidth,
                    curHeight : this.curHeight
                }
            });
        },
        getInputCmp : function () {
            return Ext.get("inputDiv");
        },
        resizable : function (b) {
            var a = new Ext.Resizable(b, {
                    wrap : true,
                    pinned : false,
                    dynamic : true,
                    minWidth : 200,
                    minHeight : 100,
                    draggable : true
                });
            a.el.setStyle("position", "absolute");
            a.on("resize", this.resizeAction, this);
        },
        resizeAction : function (f, c, j, h) {
            var b = Ext.get("basicDiv");
            var a = f.el.getBox();
            var i = f.resizeChild.id;
            var g = this.dataView.indexOf(i);
            var d = this.dataView.store.getAt(g);
            this.curWidth = c;
            this.curHeight = j;
            this.curTipsId = d.data.tipsId;
            d.set("disleft", a.x - b.getLeft());
            d.set("distop", a.y - b.getTop());
            this.store.fireEvent("recordchange", this.store);
        },
        clickView : function (c, d, f, g) {
            var h = f.id;
            var b = Ext.get(h + "-rzwrap");
            var a = c.store.getAt(d);
            this.curWidth = a.data.diswidth;
            this.curHeight = a.data.disheight;
            this.curTipsId = a.data.tipsId;
            this.maxLevel = this.maxLevel + 1;
            b.setStyle("z-index", this.maxLevel);
        },
        mouseenter : function (a, b, d, f) {
            var g = d.id;
            var c = Ext.get(g + "a");
            c.setStyle("visibility", "visible");
        },
        mouseleave : function (j, k, d, l) {
            var a = Ext.get("basicDiv");
            var n = d.id;
            var b = Ext.get(n + "a");
            if (b) {
                b.setStyle("visibility", "hidden");
                var i = Ext.get(n + "-rzwrap").getBox();
                var k = j.indexOf(n);
                var h = j.store.getAt(k);
                var c = i.x - a.getLeft();
                var m = i.y - a.getTop();
                var f = h.data.disleft;
                var g = h.data.distop;
                if ((c != f) && (m != g)) {
                    h.set("disleft", c);
                    h.set("distop", m);
                    this.store.fireEvent("recordchange", this.store);
                }
            }
        },
        deleteTips : function (c, a) {
            var b = "您确认要删除所选记录吗？";
            if ("all" == c) {
                b = "您确认要清除所有便签吗?";
            }
            Ext.Msg.confirm("信息确认", b, function (d) {
                if (d == "yes") {
                    Ext.Ajax.request({
                        url : __ctxPath
                         + "/info/multiDelAppTips.do",
                        params : {
                            ids : c,
                            names : a
                        },
                        method : "POST",
                        scope : this,
                        success : function (g, i) {
                            var j = (c == "all" ? "所有" : "");
                            Ext.ux.Toast.msg("操作信息", "成功删除" + j
                                 + "便签！");
                            if (c != "all") {
                                var f = Ext.getCmp("dataView");
                                var h = f.indexOf(a);
                                var e = Ext.get(a + "-rzwrap");
                                Ext.destroy(e);
                                f.store.removeAt(h);
                                return;
                            }
                            Ext.getCmp("PersonalTipsView").store.reload({
                                params : {
                                    start : 0,
                                    limit : 1000
                                }
                            });
                        },
                        failure : function (e, f) {
                            Ext.ux.Toast.msg("操作信息", "操作出错，请联系管理员！");
                        }
                    });
                }
            });
        }
    });
